function loginSubmit() {
    var loginId = $("#loginId").val();
    var randomCode = $("#loginRanCode").val();
    var mobileCode = $("#loginMobileCode").val();
    if (loginId === "") {
        $("#loginId").focus();
        return layer.alert("请输入手机号码/用户名称！");
    }
    if (randomCode === "") {
        $("#loginRanCode").focus();
        return layer.alert("请输入验证码！");
    }
    if (mobileCode === "") {
        $("#loginMobileCode").focus();
        return layer.alert("请输入手机登录码！");
    }
    $.ajax({
        url: '/auth/login.json',
        type: 'post', dataType: 'json',
        data: {
            "ranCode": randomCode
            , "loginId": loginId
            , "mobileCode": mobileCode
        },
        success: function (result) {
            if (result.code === 0) {
                location.href = "/default.html";
            } else {
                $("#loginRanCode").val("");
                $("#captchaText").src = "/auth/captcha/text?random=" + Math.random();
                layer.alert(result.msg);
            }
        },
        error: function (e) {
            layer.alert("send error!");
        },
        complete: function () {
        }
    });
}

function loginSubmitPwd() {
    var loginId = $("#loginId").val();
    var randomCode = $("#loginRanCode").val();
    var password = $("#password").val();
    if (loginId === "") {
        $("#loginId").focus();
        return layer.alert("请输入手机号码/用户名称！");
    }
    if (randomCode === "") {
        $("#loginRanCode").focus();
        return layer.alert("请输入验证码！");
    }
    if (password === "") {
        $("#password").focus();
        return layer.alert("请输入密码！");
    }
    $.ajax({
        url: '/auth/pwd/login.json',
        type: 'post', dataType: 'json',
        data: {
            "ranCode": randomCode
            , "loginId": loginId
            , "password": password
        },
        success: function (result) {
            if (result.code === 0) {
                location.href = "/default.html";
            } else {
                $("#loginRanCode").val("");
                $("#captchaText").src = "/auth/captcha/text?random=" + Math.random();
                layer.alert(result.msg);
            }
        },
        error: function (e) {
            layer.alert("send error!");
        },
        complete: function () {
        }
    });
}

//发送手机登录码
function sendMobileCode() {
    var loginId = $("#loginId").val();
    var randomCode = $("#loginRanCode").val();
    if (loginId === "") {
        $("#loginId").focus();
        return layer.msg("请输入手机号码/用户名称！", {icon: 2});
    }
    if (randomCode === "") {
        $("#loginRanCode").focus();
        return layer.msg("请输入验证码！", {icon: 2});
    }
    $.ajax({
        url: '/auth/login/code.json',
        type: 'post', dataType: 'json',
        data: {"ranCode": randomCode, "loginId": loginId},
        success: function (result) {
            if (result.code === 0) {
                var timeNum = 60; /*当点击获取验证码后设置60秒计时不可点击*/
                //设置按钮不可点击
                $("#btnSend").addClass("disabled").attr("onclick", "").text(timeNum + "秒后可点击");
                var timeTicket = setInterval(function () {
                    if (timeNum > 1) {
                        timeNum--;
                        $("#btnSend").text(timeNum + "秒后可点击");
                    } else {
                        $("#btnSend").removeClass("disabled").attr("onclick", "sendMobileCode()").text("获取登录码");
                        clearInterval(timeTicket);
                    }
                }, 1000);
            } else {
                $("#loginRanCode").val("");
                $("#captchaText").src = "/auth/captcha/text?random=" + Math.random();
                return layer.msg(result.msg, {icon: 2});
            }
        },
        error: function (e) {
            return layer.msg("send error!", {icon: 2});
        },
        complete: function () {
        }
    });
}

//切换密码登录方式
function switchPwd() {
    $("#login-code").hide();
    $("#pwd-show").show();
    //$("#selectLogin").removeAttr("onclick");
    $("#selectLogin").attr("onclick","switchMobile()").text("~切换手机码登录");
    //$("#submit-but").removeAttr("onclick");
    $("#submit-but").attr("onclick","loginSubmitPwd()");
    $(".login-box-msg").text("请输入您的管理账号和密码");
}
//切换验证码登录方式
function switchMobile() {
    $("#login-code").show();
    $("#pwd-show").hide();
    $("#selectLogin").attr("onclick","switchPwd()").text("~切换密码登录");
    $("#submit-but").attr("onclick","loginSubmit()");
    $(".login-box-msg").text("请输入您的管理账号和登录码");
}